#ifndef CRPC_SERVER_H
#define CRPC_SERVER_H

#include "common.h"
#include "context.h"
#include "evloop.h"
#include "hash_table.h"
#include "net.h"
#include "serial.h"

#ifdef __cplusplus
extern "C" {
#endif

struct Node {
    Context *context;
    struct Node *next;
};

// 事件注册
struct EventNode {
    int event;
    struct Node *head;
};

typedef struct RpcServer {
    int listenFd;
    int eventFd[2];// 用于事件上报内部fd
    EventLoop *loop;
    HashTable *clients;
    pthread_mutex_t mutex;
    int events[100];
    int nEvents;
    struct EventNode eventNode[100];
} RpcServer;

RpcServer *CreateRpcServer(const char *path);
int RunRpcLoop(RpcServer *server);
void ReleaseRpcServer(RpcServer *server);
int RegisterCallback(RpcServer *server, int event, Context *context);
int UnRegisterCallback(RpcServer *server, int event, const Context *context);
int EmitEvent(RpcServer *server, int event);
int OnTransact(RpcServer *server, Context *context);
int OnCallbackTransact(const RpcServer *server, int event, Context *context);
int EndCallbackTransact(const RpcServer *server, int event);

#ifdef __cplusplus
}
#endif
#endif